// Controller

function itemToRepr(item, includeChildren) {
    var itemRepr = {
        name: item.name, 
        displayPath: item.displayPath,
        isContainer: item.isContainer,
        id: item.nodeRef.id,
        url: item.url
    };
    if (item.isContainer && includeChildren) {
        itemRepr.children = item.children.map(function(child) { 
            return itemToRepr(child, recursive) 
        }, item.children);
    }
    return itemRepr;
}

if (args.folder == null) {
   status.code = 400;
   status.message = "Folder not specified.";
   status.redirect = true;
}
var folderName = args.folder;
var recursive = args.recursive && args.recursive.toLowerCase() == "yes" ? true : false;
var folder = roothome.childByNamePath(folderName);
if (folder == null || !folder.isContainer) {
   status.code = 404;
   status.message = "Folder " + folderName + " not found.";
   status.redirect = true;
}

model.folder = folder;
model.folderJSON = jsonUtils.toJSONString(itemToRepr(folder, true));